
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.service.IFtpServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.model.FtpServer;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class FtpServerAction extends AbstractAdminController<IFtpServerService>{

    @Autowired
    private IFtpServerService ftpServerService;
    
    @RequestMapping(value="/ftpserver/list")
    public String list(FtpServer ftpServer,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(ftpServer); 	
    	BizData4Page<FtpServer> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("ftpServer", ftpServer);
    	return "/module/ftpserver/list";
    }    
    
    @RequestMapping(value="/ftpserver/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("ftpServer", new FtpServer());
        return "module/ftpserver/edit";
    }    
    
    @RequestMapping(value="/ftpserver/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	FtpServer ftpServer = ftpServerService.fetch(id);
    	model.addAttribute("ftpServer", ftpServer);
        return "module/ftpserver/edit";
    }    
    
    @RequestMapping(value="/ftpserver/save")
    @ResponseBody
    public String save(HttpServletRequest request,FtpServer ftpServer){
		boolean isAddNew = StringUtils.isBlank(ftpServer.getId())?true:false;
		if(isAddNew)
		{
			ftpServer.setId(IdGenerator.createNewId());
			ftpServerService.add(ftpServer);
		}
		else
		{
			ftpServerService.update(ftpServer);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/ftpserver/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	ftpServerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(FtpServer ftpServer)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("groupId", ftpServer.getGroupId());		
		conditions.put("ftpType", ftpServer.getFtpType());		
		conditions.put("ftpAlias", ftpServer.getFtpAlias());		
		conditions.put("ftpUser", ftpServer.getFtpUser());		
		conditions.put("ftpHost", ftpServer.getFtpHost());		
		conditions.put("ftpPass", ftpServer.getFtpPass());		
		conditions.put("ftpPort", ftpServer.getFtpPort());		
		conditions.put("createDate", ftpServer.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IFtpServerService getMainService() {
        return ftpServerService;
    }

    @Override
    protected String getMainObjName() {
        return "ftpserver";
    }

    @Override
    protected String getViewTitle() {
        return "ftpserver";
    }
}
